{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

L'affectation.

\n", "\n", "

L'affection se fait par l'intermédiaire du symbole =.

\n", "

Pour valider une cellule, il faut utiliser shift + entrée.

\n", "

Vous pouvez modifier les valeurs pour faire des tests.

\n", "

Pensez à alimenter votre ahier de bors des différentes traces de vos recherches et exercices

" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "a=5\n", "b=3\n", "c=a+b" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "a=b=c=4" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 4, 4)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a,b,c" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "# L'affectation multiple\n", "a,b=4,5" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 5)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a,b" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Une curiosité PYTHON, cela ne fonctionne pas dans les autres langages\n", "\n", "a,b=1,2\n", "\n", "a,b=b,a\n", "\n", "a,b" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# La commande type permet de connaître le type de la variable.\n", "# PYTHON est un langage autotypé. Il créer le type au moment de l'affectation. \n", "\n", "a=7\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Trouver les types des variables ci dessous : \n", "\n", "a=6\n", "b=4.5\n", "c=\"hello world !\"\n", "d=True\n", "e=(1,4,5,6)\n", "f=[3,5,7]\n", "g={\"Paul\":7,\"Marc\":17,\"Marie\":8}\n", "h={2,6,7,9}\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vous pouvez réaliser des recerches sur ces différents types de variables.\n", "Faites des manipulations avec ces différents types.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Les fonctions.

" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def carre(x:float)->float:\n", " return x*x" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Pour appeler la fonction, il faut utiliser carre(4) par exemple.\n", "\n", "carre(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vous pouvez utiliser une variable comme résultat d'une fonction." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(25, 625)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=carre(5)\n", "\n", "b=carre(carre(5))\n", "\n", "a,b" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function cube in module __main__:\n", "\n", "cube(x: float) -> float\n", " Cette fonction retourne le cube du nombre entré.\n", "\n" ] } ], "source": [ "# Utilisation des docstrings. Vous pouvez écrire un commentaire qui pourra être appelé avec la fonction help.\n", "\n", "def cube(x:float)->float:\n", " \"\"\"Cette fonction retourne le cube du nombre entré. \"\"\"\n", " return x*x*x\n", "\n", "help(cube)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#On peut utiliser une fonction comme paramêtre d'une fonction\n", "\n", "def f(x:float)->float:\n", " return x+4\n", "\n", "def g(x:float)->float:\n", " return x*x+1\n", "\n", "def substitution(x:float,f,g)->float :\n", " return f(g(x))\n", "\n", "x=5\n", "substitution(x,f,g)\n", "\n", "# Ce calcul correspond à 5*5+1+4 pour x=5" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# la commande assert\n", "\n", "def discriminant(a,b,c:float)-> float :\n", " assert (a != 0)\n", " delta=b*b-4*a*c\n", " return delta\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Faire un appel avec discriminant(0,-4,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Différence entre fonction et procédure.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "La différence entre fonction et procédure vient du fait que la procédure ne renvoie rien.
\n", "Le langage PYTHON ne fait pas la différence entre fonction et procédure (dans certains langages le deux mots clés existent).
\n", "Par exemple si vous traitez les affichages à part :
" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Bonjour Pascal THERESE\n" ] } ], "source": [ "def affichage(nom,prenom :str)->None :\n", " print(\"Bonjour \",prenom,\" \",nom)\n", " return None\n", "\n", "affichage(\"THERESE\",\"Pascal\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vous remarquerez None (qui n'est pas obligatoire en PYTHON) qui indique que la fonction ne renvoie rien. " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Bonjour Pascal THERESE\n" ] } ], "source": [ "def affichage(nom,prenom :str)->None :\n", " print(\"Bonjour \",prenom,\" \",nom)\n", " \n", " # None a été retiré\n", "\n", "affichage(\"THERESE\",\"Pascal\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Quelques exemples à traiter

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Quelques exercices à réaliser

\n", " \n", "
  • Ecrire une fonction bonjour avec comme argument un mot qui affiche bonjour suivi du mot.
    \n", " Exemple : bonjour(\"Paul\")-> bonjour Paul
  • \n", "
  • Ecrire une fonction moyenne qui renvoie la moyenne de quatre nombres passés en arguments.
  • \n", "
  • Ecrire en PYTHON les algorithmes utilisés dans le td_1 d'algorithmique en pseudo code
    Attention, certains algorithmes utilisent des qtructures que vous verrez par la suite.
  • \n", "
    \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }